﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;

namespace COUP.Core.Files
{
    public static class ItemType
    {  
        public static Dictionary<uint, ItemTypeEntry> Parse()
        {
            Dictionary<uint, ItemTypeEntry> _Itemdata = new Dictionary<uint, ItemTypeEntry>();

            while (!Monitor.TryEnter(Console.Out, 50)) { }                
            Console.Write("[Game Server] Loading Item Data...");
            int Left = Console.CursorLeft;
            FileStream File = new FileStream(Settings.ItemType, FileMode.Open);
            BinaryReader Reader = new BinaryReader(File);

            int start = System.Environment.TickCount;
            uint Amount = Reader.ReadUInt32();

            ItemTypeEntry _IDetail;

            File.Seek(4 * Amount, SeekOrigin.Current);

            for (int i = 0; i < Amount; i++)
            {
                _IDetail = new ItemTypeEntry();
                _IDetail.ID = Reader.ReadUInt32();

                Console.Write(_IDetail.ID);
                Console.CursorLeft = Left;

                for (int x = 0; x < 16; x++)
                {
                    _IDetail.Name += (char)Reader.ReadByte();
                }
                _IDetail.Name = _IDetail.Name.Trim('\0');

                _IDetail.Class = Reader.ReadByte();
                _IDetail.Prof = Reader.ReadByte();
                _IDetail.Level = Reader.ReadUInt16();
                _IDetail.Vit_Req = Reader.ReadUInt16();
                _IDetail.Str_Req = Reader.ReadUInt16();
                _IDetail.Dex_Req = Reader.ReadUInt16();
                _IDetail.Spi_Req = Reader.ReadUInt16();
                _IDetail.TradeType = (byte)Reader.ReadUInt32();
                _IDetail.Price = Reader.ReadUInt32();

                File.Seek(4, SeekOrigin.Current);

                _IDetail.Damage_Max = Reader.ReadUInt16();
                _IDetail.Damage_Min = Reader.ReadUInt16();
                _IDetail.Defense_Add = Reader.ReadUInt16();
                _IDetail.Dex_Add = Reader.ReadUInt16();
                _IDetail.Dodge_Add = Reader.ReadUInt16();
                _IDetail.HP_Add = Reader.ReadUInt16();
                _IDetail.MP_Add = Reader.ReadUInt16(); ;
                _IDetail.Dura = Reader.ReadUInt16();
                _IDetail.MaxDura = Reader.ReadUInt16();
                _IDetail.Magic_Attack = Reader.ReadUInt16();
                _IDetail.MDefense_Add = Reader.ReadUInt16();

                File.Seek(6, SeekOrigin.Current);

                _IDetail.Range = (byte)Reader.ReadUInt16();
                _IDetail.Frequency = Reader.ReadUInt16();

                for (int x = 0; x < 16; x++)
                {
                    _IDetail.Description += (char)Reader.ReadByte();
                }

                _IDetail.Description = _IDetail.Description.Trim('\0');

                if (!_Itemdata.ContainsKey(_IDetail.ID))
                    _Itemdata.Add(_IDetail.ID, _IDetail);

                File.Seek(112, SeekOrigin.Current);
            }

            Console.Write("Completed in {0}ms\n", Environment.TickCount - start);
            Monitor.Exit(Console.Out);

            File.Dispose();
            File.Close();
            Reader.Close();

            return _Itemdata;
        }
    }
}
